﻿using Microsoft.Extensions.DependencyInjection;
using System;
namespace iTPS.Service.Core.Http
{
    using Configurations;
    using Resources;
    public static class HttpClientExtension
    {
        public static IServiceCollection AddHttpClientHelper(this IServiceCollection serviceDescriptors, HttpClientUriConfiguration httpClientUriConfiguration = null)
        {
            if (httpClientUriConfiguration == null || httpClientUriConfiguration.HttpClientConfiguration == null)
            {
                serviceDescriptors.AddHttpClient();
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(httpClientUriConfiguration.HttpClientConfiguration.QRCodeDeployChannel))
                {
                    serviceDescriptors.AddQRCodeDeployChannel(httpClientUriConfiguration);
                }

                if (!string.IsNullOrWhiteSpace(httpClientUriConfiguration.HttpClientConfiguration.UCityModulePayChannel))
                {
                    serviceDescriptors.AddUCityPayChannel(httpClientUriConfiguration);
                }
            }
            serviceDescriptors.AddSingleton<HttpClientHelper>();
            return serviceDescriptors;
        }

        public static IServiceCollection AddQRCodeDeployChannel(this IServiceCollection serviceDescriptors, HttpClientUriConfiguration httpClientUriConfiguration)
        {
            serviceDescriptors.AddHttpClient(ConstHttpClientName.QRCodeDeployChannel, (client) =>
            {
                client.BaseAddress = new Uri(httpClientUriConfiguration.HttpClientConfiguration.QRCodeDeployChannel);
            });
            return serviceDescriptors;
        }

        public static IServiceCollection AddUCityPayChannel(this IServiceCollection serviceDescriptors, HttpClientUriConfiguration httpClientUriConfiguration)
        {
            serviceDescriptors.AddHttpClient(ConstHttpClientName.UCityModulePayChannel, (client) =>
            {
                client.BaseAddress = new Uri(httpClientUriConfiguration.HttpClientConfiguration.UCityModulePayChannel);;
            });
            return serviceDescriptors;
        }
    }
}
